{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../')\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import steward as st\n",
    "import matplotlib.pyplot as plt\n",
    "import pickle\n",
    "import xgboost\n",
    "from sklearn.metrics import roc_auc_score\n",
    "%matplotlib inline\n",
    "from src import build\n",
    "from src import train, config\n",
    "from src.feature_cols import to_drop\n",
    "import h5py\n",
    "import os\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "build.build_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feature_list = [\n",
    "#     'basic_preprocess/cont_r_train_30W',\n",
    "\n",
    "    'basic_preprocess/cont_train_30W',\n",
    "    'basic_preprocess/conc_train_30W',\n",
    "    \n",
    "#     'index/index_train_30W'\n",
    "    \n",
    "    'feature/rank1_train_30W',\n",
    "    'feature/rank2_train_30W',\n",
    "    'feature/rank3_train_30W',\n",
    "    \n",
    "    'feature/history1_train_30W',\n",
    "    'feature/history2_train_30W',\n",
    "    'feature/history3_train_30W',\n",
    "\n",
    "    'feature/ordnum_train_30W',\n",
    "    'feature/room1_train_30W',\n",
    "    'feature/basicroom1_train_30W',\n",
    "    'feature/orderroom1_train_30W',\n",
    "    \n",
    "    'feature/rank_history1_train_30W',\n",
    "    'feature/rank_history2_train_30W',\n",
    "    'feature/rank_history3_train_30W',\n",
    "    \n",
    "#     'feature/room1head5_train_30W',\n",
    "    'feature/room1small5_train_30W',\n",
    "    'feature/room1large5_train_30W',\n",
    "#     'feature/orderroom1head5_train_30W',\n",
    "    'feature/orderroom1small5_train_30W',\n",
    "    'feature/orderroom1large5_train_30W',\n",
    "\n",
    "    'feature/custom1_train_30W',\n",
    "    'feature/roomcount_date_train_30W',\n",
    "\n",
    "\n",
    "]\n",
    "\n",
    "y = 'basic_preprocess/y_train_30W'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "__x = []\n",
    "for name in feature_list:\n",
    "    __x.append(st.get_instance(name).load())\n",
    "y_df = st.get_instance(y).load()\n",
    "X_df = pd.concat(__x, copy=False, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "t = st.LoadInstance('test/real_all_without_head_30W')\n",
    "t()\n",
    "im = t.df['im']\n",
    "result = t.df['result']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "result = pd.DataFrame(result, columns=['n', 'iter_n', 'score'])\n",
    "top_cols = im.head(395).index.tolist()\n",
    "\n",
    "select_cols = top_cols\n",
    "feature_df = X_df[select_cols]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model_para = {\n",
    "    'objective': 'rank:pairwise',\n",
    "    'n_estimators': 155,\n",
    "#     'scale_pos_weight': 5,\n",
    "    'learning_rate': 0.2,\n",
    "    'max_depth':5\n",
    "}\n",
    "\n",
    "train.train_fold(feature_df, y_df, model_para=model_para, save=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_para = {\n",
    "    'objective': 'rank:pairwise',\n",
    "    'n_estimators': 346,\n",
    "#     'scale_pos_weight': 5,\n",
    "    'learning_rate': 0.3,\n",
    "    'max_depth':4\n",
    "}\n",
    "train.train_all(feature_df, y_df, model_para=model_para)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model_para = {\n",
    "    'objective': 'rank:pairwise',\n",
    "    'n_estimators': 1000,\n",
    "#     'scale_pos_weight': 5,\n",
    "    'learning_rate': 0.2,\n",
    "    'max_depth':4,\n",
    "}\n",
    "r = train.train_xgboost_step(feature_df, y_df, early_stopping_rounds=50, n_estimators=1000, model_para=model_para,\n",
    "                            use_piece=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
